package com.tang.client;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.StandardCharsets;

/**
 * channel初始化参数
 *
 * @author fulin.tang
 * @version V3.0
 * @since 2022/5/20 17:57
 */
public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * 初始化通道
     *
     * @param ch the {@link Channel} which was registered.
     * @throws Exception -
     */
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline().addLast(new LineBasedFrameDecoder(1024));// 1024字节换行
        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); // 配置解码器
        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); // 配置编码器
        ch.pipeline().addLast(new ClientHandler());// 给通道配上自定义的数据接收方法
    }

}
